package com.chima.action;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.Department;
import com.chima.domain.DepartmentFinace;
import com.chima.domain.Performance;
import com.chima.service.DepartmentFinaceService;
import com.chima.service.DepartmentService;
import com.chima.service.PerformanceService;
import com.opensymphony.xwork2.ActionSupport;

public class ExportExecleAction extends ActionSupport {
	private int dept;
	private String searchdate;
	private DepartmentService departmentService;
	private DepartmentFinaceService deptFinaceService;
	private List<DepartmentFinace> departmentFinaces;
	public DepartmentFinaceService getDeptFinaceService() {
		return deptFinaceService;
	}

	public void setDeptFinaceService(DepartmentFinaceService deptFinaceService) {
		this.deptFinaceService = deptFinaceService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public String getSearchdate() {
		return searchdate;
	}

	public void setSearchdate(String searchdate) {
		this.searchdate = searchdate;
	}

	public int getDept() {
		return dept;
	}

	public void setDept(int dept) {
		this.dept = dept;
	}
	private PerformanceService performanceService ;
	
	public PerformanceService getPerformanceService() {
		return performanceService;
	}

	public void setPerformanceService(PerformanceService performanceService) {
		this.performanceService = performanceService;
	}

	@Override
	public String execute() throws Exception {
		
			if(searchdate!=null){
				
				departmentFinaces=deptFinaceService.findDepartmentFinaceByDate(searchdate);
			}
		
		
		// 第一步 创建文档对象
		Workbook wb = new HSSFWorkbook();
		// 第二步 创建sheet
		Sheet sheet = wb.createSheet("各科室绩效详细信息");
		// 第三步 写数据
		// 标题
		Row headRow = sheet.createRow(0);
		String[] heads = { "科室名称","奖金","日期"};
		    CellStyle style =wb.createCellStyle();
		    Font f  = wb.createFont();      
		    f.setFontHeightInPoints((short) 16);//字号      
		    f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗      
		    style.setFont(f);      
		    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中      
		    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框    
		    style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框       
		    style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框       
		  
		    CellStyle style1 =wb.createCellStyle();
		    Font f1  = wb.createFont();      
		    f1.setFontHeightInPoints((short) 10);//字号     
		
		    style1.setFont(f1);      
		    style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中      
		    style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框    
		    style1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
		    style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框       
		    style1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框      
		    
		for (int i = 0; i < heads.length; i++) {
			Cell cell = headRow.createCell(i);
			headRow.setHeight((short) 400);
			cell.setCellValue(heads[i]);
			cell.setCellStyle(style);
		    sheet.setColumnWidth(i, 4300);
		}

		// 内容生成
		for (int i = 0; i < departmentFinaces.size(); i++) {
			// 从第二行开始
			Row row = sheet.createRow(i+1);
			DepartmentFinace p=departmentFinaces.get(i);
			
			String[] contents = { p.getDepartment(),String.valueOf(p.getDeptFinace()),p.getDate()};
			for (int j = 0; j < contents.length; j++) {
				Cell cell = row.createCell(j);
				cell.setCellValue(contents[j]);
				cell.setCellStyle(style1);
			}
		}

		String fname=new String("各科室绩效详细信息".getBytes(), "ISO8859-1");
		String fileName=fname+searchdate+".xls";
  		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			OutputStream os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition", "attachment; filename="+fileName);
			// 设定输出文件头
			response.setContentType("application/msexcel;charset=UTF-8");// 定义输出类型
			wb.write(os);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return NONE;
	}

}

